/**
 * 
 */
package com.pactera.android.business;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.util.Log;

import com.pactera.android.db.ContactListDB;
import com.pactera.android.util.Constants;
import com.pactera.android.util.HttpUtil;

/**
 * @author David Zheng
 *
 */
public class ContactListBusiness {
	
	static final String TAG = "ContactListBusiness";
	
	private Cursor contactListCursor;
	
	private ContactListDB contactListDB;
	
	
	//private ConfigPreference preference;
	
	private String[] columns = {ContactListDB.ID, ContactListDB.NAME, ContactListDB.MOBILE};
	
	
	public ContactListBusiness(Context context) {
		this.contactListDB = new ContactListDB(context);
		contactListDB.OpenDB();
	}
	
	/**
	 * loads the data either from remote or from local
	 * @return
	 */
	public Cursor loadData() {
		contactListCursor = getLocalData();

		if (contactListCursor != null && contactListCursor.getCount() > 0) {
		
		} else if (syncData()) 
			contactListCursor = getLocalData();
		
		return contactListCursor;

	}
	
	/**
	 * To sync the local data with the remote server
	 * @return
	 */
	public boolean syncData(){
		List<ContentValues> info = getRemoteData();
		boolean result = false;
		if (info != null) {
			updateContact(info);
			result = true;
		}
		
		return result;
	}
	
	
	public void releaseResource() {
		contactListDB.CloseDb();
	}
	/**
	 * sends the request to get the remote data
	 * @return
	 */
	private List<ContentValues> getRemoteData(){		
		//int version = mPreference.getVersion();	
		Log.d(TAG, "Load Data from remote");
		List<ContentValues> list = new ArrayList<ContentValues>();
		try {
			String result = HttpUtil.postRequest(Constants.SERVER_URL + Constants.CONTACT_LIST_URI, null);
			Log.d(TAG, result);
			
			//JSONArray jsonArray = new JSONArray(result);
			JSONObject jsonResponse = new JSONObject(new String(result));
			JSONArray jsonArray = jsonResponse.getJSONArray("contactList");
			
			Log.d(TAG, "length:"+jsonArray.length());
			for (int i = 0; i < jsonArray.length(); i++) {
				
				JSONObject jsonObject = jsonArray.getJSONObject(i);
				ContentValues values = new ContentValues();
				values.put(ContactListDB.ID, jsonObject.getString("id"));
				values.put(ContactListDB.NAME, jsonObject.getString("alias"));				
				values.put(ContactListDB.MOBILE, jsonObject.getString("mobile"));
				Log.d(TAG, jsonObject.getString("alias"));

				list.add(values);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return list;

	}
	
	/**
	 * gets the data from local DB.
	 * @return
	 */
	private Cursor getLocalData(){	
		Log.d(TAG, "Load Data from local");
		Cursor list = contactListDB.query(Constants.CONTACT_LIST_TABLE, columns, null, null, null, null, ContactListDB.NAME);
		return list;
	}
	
	/**
	 * Adds contact list to DB
	 * @param info
	 */
	private void updateContact(List<ContentValues> info){	
		Log.d(TAG, "updateContact");
		for(int i = 0; i<info.size(); i++){				
			ContentValues values = info.get(i);				
			contactListDB.insertOrUpdate(Constants.CONTACT_LIST_TABLE, null, values);
		}
	}
	
	/**
	 * 
	 * @return
	 */
	public Cursor getCursor() {
		return contactListCursor;
	}
	
}
